CC		= gcc.exe
LD  	= ld.exe
ASM		= nasm.exe
MAKE	= make.exe
PYTHON 	= py.exe
RM 		= del
CFLAGS	= -c -m32 -nostdlib -fno-stack-protector -fno-builtin -W -Wall -Wstrict-prototypes -Wmissing-prototypes
LDFLAGS	= -Ttext 0xc0400000 -e __main -m i386pe -s

BUILD_PATH	= build
KERNEL_PATH	= kernel
LIB_PATH	= lib
DEV_PATH	= dev
FS_PATH		= fs
ASM_PATH	= asm

DEVOBJS		:= $(BUILD_PATH)/hal.o $(BUILD_PATH)/ega.o $(BUILD_PATH)/keybd.o $(BUILD_PATH)/disk.o
LIBOBJS		:= $(BUILD_PATH)/rtl.o $(BUILD_PATH)/math.o $(BUILD_PATH)/fifo.o $(BUILD_PATH)/heap.o
KEROBJS		:= $(BUILD_PATH)/main.o $(BUILD_PATH)/table.o $(BUILD_PATH)/memory.o
FSOBJS		:= $(BUILD_PATH)/file.o
ASMOBJS		:= $(BUILD_PATH)/int.o $(BUILD_PATH)/res.o

OBJFORM		= win32

OBJS		:= $(DEVOBJS) $(LIBOBJS) $(KEROBJS) $(FSOBJS) $(ASMOBJS)

EXE			= main.exe

ARCH 		= x86
IMG			= hd.img
VM			= qemu.exe

.PHONY:all clean exec build rebuild reexec run rerun

all:$(IMG)

exec:$(IMG)
	$(VM) -hda $(IMG)

build:all

rebuild:clean build

reexec:clean exec

run:exec

rerun:reexec

$(IMG):$(EXE) arch/$(ARCH)/mbr.bin arch/$(ARCH)/loader.bin
	$(PYTHON) make.py $(IMG) 0 arch/$(ARCH)/mbr.bin 2 arch/$(ARCH)/loader.bin 16 $(EXE)

arch/$(ARCH)/mbr.bin:arch/$(ARCH)/mbr.S
	$(ASM) $< -o $@

arch/$(ARCH)/loader.bin:arch/$(ARCH)/loader.S
	$(ASM) $< -o$@

$(EXE):$(OBJS)
	$(LD) $(OBJS) $(LDFLAGS) -o $(EXE)

$(DEVOBJS): $(BUILD_PATH)/%.o:$(DEV_PATH)/%.c $(DEV_PATH)/%.h
	$(CC) $(CFLAGS) $< -o $@

$(LIBOBJS): $(BUILD_PATH)/%.o:$(LIB_PATH)/%.c $(LIB_PATH)/%.h
	$(CC) $(CFLAGS) $< -o $@

$(KEROBJS): $(BUILD_PATH)/%.o:$(KERNEL_PATH)/%.c $(KERNEL_PATH)/%.h
	$(CC) $(CFLAGS) $< -o $@

$(FSOBJS): $(BUILD_PATH)/%.o:$(FS_PATH)/%.c $(FS_PATH)/%.h
	$(CC) $(CFLAGS) $< -o $@

$(ASMOBJS): $(BUILD_PATH)/%.o:$(ASM_PATH)/%.S
	$(ASM) $< -o $@ -f $(OBJFORM)

clean:
	$(RM) build\*.o
	$(RM) $(IMG) $(EXE)
	$(RM) arch\$(ARCH)\mbr.bin arch\$(ARCH)\loader.bin
